package com.hunk.core.auth.entity;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hunk.core.entity.UUIDEntity;

/** Role Information
 * Description :<br>
 * 
 * @author HunkDeng
 * @since 2013-4-13
 */
@Entity
@Table(name="SYS_ROLE")
public class Role extends UUIDEntity {
	
	private static final long serialVersionUID = -867269960737851312L;
	@Column(name = "ROLE_NAME", nullable = false, unique = true)
	@Size(min=5, max=15, message="角色名为5-15个字以内")
	private String roleName;
	@Column
	private String description;
	@Column
    private Boolean isSystem = false;
	@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "PARENT_ROLE_ID")
    private Role parentRole;
	
	/**
     * persistent field
     */
	@JsonIgnore
    @OneToMany(mappedBy = "parentRole",fetch = FetchType.LAZY)
	@OrderBy("roleName ASC")
    private Set<Role> childRoles = new LinkedHashSet<Role>();
    
	public Role() {
	}

	public Role(String roleName) {
		this.roleName = roleName;
	}
	
	// setter and getter
	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getIsSystem() {
        return isSystem;
    }

    public void setIsSystem(Boolean isSystem) {
        this.isSystem = isSystem;
    }

    public Role getParentRole() {
        return parentRole;
    }

    public void setParentRole(Role parentRole) {
        this.parentRole = parentRole;
    }
    @JsonIgnore
	public Set<Role> getChildRoles() {
		return childRoles;
	}
    @JsonIgnore
	public void setChildRoles(Set<Role> childRoles) {
		this.childRoles = childRoles;
	}

	@Override
	public String toString() {
		return "Role [id=" + getId() + ", roleName=" + roleName + ", description="
				+ description + ", isSystem=" + isSystem + ", parentRole="
				+ parentRole + "]";
	}

}
